<?php

namespace App\Models;

use Log;

class VoiceSearchWeather
{
    public $city = '北京';

    public function getUrl()
    {
       return 'http://api.map.baidu.com/telematics/v3/weather?location='.urlencode($this->city).'&output=json&ak=11ffd27d38deda622f51c9d314d46b17';
    }

    public function getContent()
    {
        $client = new \GuzzleHttp\Client(['timeout' => 2.0]);
        $response = $client->request('GET', $this->getUrl());
        $htmlContent = $response->getBody()->getContents();
        $jsonContent = json_decode($htmlContent, true);
        if($jsonContent && isset($jsonContent['results']) && isset($jsonContent['results'])) {
            return $jsonContent['results'];
        } else {
            return null;
        }
    }

    public function today()
    {
        $weatherData = $this->getContent();
        if($weatherData) {
            return  [
                'status' => 0,
                'message' => '',
                'datas' => $weatherData
            ];
        } else {
            return  [
                'status' => 1002,
                'message' => '系统错误,请稍后重试',
                'data' => null
            ];
        }
    }
}
